package com.md.model.system;

import com.md.base.BaseClient;
import com.md.base.exception.ServerException;
import com.md.util.HttpUtil;
import com.md.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author 刘骄阳
 * 2024-10-25 23:30
 */
@AllArgsConstructor
@Data
public abstract class BaseOpenController {

  private BaseClient baseClient;
  private String endpoint;
  private String serverName;
  private String basePath;

  protected String getEndpoint() {
    if (ObjectUtil.isEmpty(endpoint) && ObjectUtil.notEmpty(serverName)) {
      return HttpUtil.buildServiceUrl(serverName);
    }
    return endpoint;
  }

  public String buildPostUrl(final String path) {
    if (path.startsWith("/")) {
      return getEndpoint() + basePath + path;
    }
    return getEndpoint() + basePath + "/" + path;
  }

  public <T> CommonResult<T> checkResponse(final CommonResult<T> result) {
    if (result.isSuccess()) {
      return result;
    }
    throw ServerException.build(result.getCode(), result.getMessage());
  }
}
